﻿using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage.SysMng
{
    /// <summary>
    /// UserView.xaml 的交互逻辑
    /// </summary>
    public partial class UserView : UserControl
    {
        List<User> bindUser = null;

        public UserView()
        {
            InitializeComponent();
            this.Loaded += UserView_Loaded;
        }

        private void UserView_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                RefData();
            }
        }

        private void RefData()
        {
            bindUser = ManagerDBCtrl.GetMng().GetUserInfo("");
            ListViewStudent.ItemsSource = bindUser;
            GridView gv = ListViewStudent.View as GridView;
            if (gv != null)
            {
                foreach (GridViewColumn gvc in gv.Columns)
                {
                    gvc.Width = gvc.ActualWidth;
                    gvc.Width = Double.NaN;
                }
            }
        }

        private void blt_resetPwd_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            User user = blt.DataContext as User;
            if (user != null)
            {
                EditUserInfo userEdit = new EditUserInfo();
                userEdit.SetResetPassword(user);
                userEdit.ShowDialog();
            }
        }

        private void blt_edituser_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            User user = blt.DataContext as User;
            if (user != null)
            {
                EditUserInfo userEdit = new EditUserInfo();
                userEdit.SetEditUserInfo(user);
                if (userEdit.ShowDialog() == true)
                {
                    RefData();
                }
            }
        }

        private void blt_deluser_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            User user = blt.DataContext as User;
            if (user != null)
            {
                if (ManagerDBCtrl.GetMng().DelUserInfo(user.id))
                {
                    RefData();
                }
            }
              
        }

        private void blt_newuser_Click(object sender, RoutedEventArgs e)
        {
            EditUserInfo userEdit = new EditUserInfo();
            if (userEdit.ShowDialog() == true)
            {
                RefData();
            }
        }
    }
}
